//
//  SNCommentViewController.m
//  DayDayHappy
//
//  Created by yutao on 14-2-23.
//  Copyright (c) 2014年 yutao. All rights reserved.
//

#import "SNCommentViewController.h"
#import "SNCommentModel.h"
#import "SNCommentCell.h"

#import "SNCommentToolBar.h"
#import "SNCommentTextView.h"


#define kCommentListKey     @"comment_list"



@interface SNCommentViewController ()<SNCommentToolBarDelegate,SNCommentTextViewDelegate>
{
    SNCommentTextView *_textView;
    int _page;
}

@end

@implementation SNCommentViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
//        self.requestUrl = [NSString stringWithFormat:@"%@%@?type=%@&id=%d",kBaseUrl,kGetCommentList,kPleasureModle,1];
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad
{
    self.requestUrl = [NSString stringWithFormat:@"%@%@?type=%@&id=%d",kBaseUrl,kGetCommentList,kPleasureModle,self.model.content_id];
    [super viewDidLoad];
    
    _page = 1;
    self.title = @"评论列表";
    
    [self initBackBarButton];
    
    [self initCommentToolBar];
}

- (SNBaseModel *)currModelWithObj:(id)obj
{
    return [[SNCommentModel alloc] initWithDictionary:obj error:nil];
}


- (NSString *)createNextPageRequestUrl
{
    return @"";
}

- (void)initCommentToolBar
{
    SNCommentToolBar *toolBar = [[SNCommentToolBar alloc] initWithFrame:CGRectMake(0, kDeviceHeight - kCommentToolBarHeight - ([self isiOS7] ? 0 : 64), kDeviceWidth, kCommentToolBarHeight)];
    toolBar.delegate = self;
    [self.view addSubview:toolBar];
}


- (void)initBackBarButton
{
    
    self.navigationItem.leftBarButtonItem = nil;
    UIButton * backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setFrame:CGRectMake(15, 0, 60, 40)];
    [backBtn setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *menuBarButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = menuBarButton;
}

- (void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark ====================UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contentDataArray.count;
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return CGRectGetHeight([self tableView:tableView cellForRowAtIndexPath:indexPath].frame);
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentif = @"cell";
    SNCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentif];
    if (cell == nil) {
        cell = [[SNCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentif];
    }
    [cell setUpCommentCellWithObject:self.contentDataArray[indexPath.row]];
    return cell;
    
    return nil;
}

#pragma mark ====================SNCommentToolBarDelegate

- (void)commentToolBarDidSelected:(SNCommentToolBar *)tooBar
{
    if (_textView == nil) {
      _textView = [[SNCommentTextView alloc] initWithFrame:CGRectMake(0, 0, kDeviceWidth, kTextViewHeight)];
        _textView.delegate = self;
    }
    [_textView show];
}

#pragma mark ====================SNCommentTextViewDelegate

- (void)sendButtonDidClickedWithText:(NSString *)text;
{
    
    /*
     uid:用户id
     type:搞笑=pleasure 唯美=beauty 唯美=video
     content_id:帖子id
     content:评论内容
     */
    
    
    NSDictionary *entity = [[NSUserDefaults standardUserDefaults] objectForKey:kLoginAccount];
    

    
    [self postCommentWithUserInfo:entity text:text];
}

- (void)postCommentWithUserInfo:(NSDictionary *) entity text:(NSString *)text
{
    [_textView hide];
    NSDictionary *userParms = @{@"uid":[entity objectForKey:@"uid"],@"type":self.model.type,@"content_id":@(self.model.content_id),@"content":text};
    
    AFHTTPClient * httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:kBaseUrl]];
    [httpClient setParameterEncoding:AFFormURLParameterEncoding];
    [httpClient postPath:kSetComment parameters:userParms success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"===ERROR===*****%@",[error description]);
    }];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
